/**
 ******************************************************************************
 * @file    vsmd.h
 * @author  www.vincetech.com
 * @date    2020/05/15
 * @brief   vsmd lib
 ******************************************************************************
 * @attention
 *
 * 
 *
 ******************************************************************************
 */ 

#ifndef _VSMD_H_
#define _VSMD_H_

/* Includes ------------------------------------------------------------------*/
#include "string.h"
#include "stdio.h"
#include "stddef.h" 
#include "math.h"

/**
 * @brief the method need be called while 1 can frame is recieved.
 * @param eid extern data frame id
 * @param dlc data length
 * @param data data
 */
extern void _on_recieved(unsigned int eid, unsigned char dlc, unsigned char* data);
/**
 * @brief the method need be called in 1ms sys_tick or 1ms timer
 */
extern void _on_tick(void);

/**
 * @brief enable motor (power on)
 * @param cid communication id of vsmd device
 */
extern void vsmd_enable(unsigned char cid);
/**
 * @brief disable motor (power off)
 * @param cid communication id of vsmd device
 */
extern void vsmd_disable(unsigned char cid);
/**
 * @brief set current position as 0 position
 * @param cid communication id of vsmd device
 */
extern void vsmd_org(unsigned char cid);
/**
 * @brief motor stop
 * @param cid communication id of vsmd device
 */
extern void vsmd_stop(unsigned char cid);
/**
 * @brief motor move (speed mode)
 * @param cid communication id of vsmd device
 */
extern void vsmd_move(unsigned char cid);
/**
 * @brief motor move (position mode)
 * @param cid communication id of vsmd device
 * @param pos target position
 */
extern void vsmd_moveto(unsigned char cid, int pos);
/**
 * @brief motor relative move (position mode)
 * @param cid communication id of vsmd device
 * @param dis distance
 */
extern void vsmd_rmove(unsigned char cid, int dis);
/**
 * @brief motor start homing
 * @param cid communication id of vsmd device
 */
extern void vsmd_zero_start(unsigned char cid);
/**
 * @brief motor stop homing
 * @param cid communication id of vsmd device
 */
extern void vsmd_zero_stop(unsigned char cid);
/**
 * @brief io output (sensor as output)
 * @param cid communication id of vsmd device
 * @param snr_id sensor id (0-s1 1-s2 2-s3 3-s4 4-s5 5-s6)
 * @param on_off 0-low level 1-high level
 */
extern void vsmd_output(unsigned char cid, unsigned char snr_id, unsigned char on_off);
/**
 * @brief solenoid valve / brake
 * @param cid communication id of vsmd device
 * @param nmos_id id (0-nmos1 1-nmos2 2-nmos3)
 * @param dr dutyratio (0-255), reduce the heat while using suitable dutyratio
 */
extern void vsmd_sv(unsigned char cid, unsigned char nmos_id, unsigned char dr);
/**
 * @brief nmos
 * @param cid communication id of vsmd device
 * @param nmos_id id (0-nmos1 1-nmos2 2-nmos3)
 * @param dr dutyratio (0-255)
 */
extern void vsmd_nmos(unsigned char cid, unsigned char nmos_id, unsigned char dr);
/**
 * @brief shake between 2 positions
 * @param cid communication id of vsmd device
 * @param spos start position
 * @param epos end position
 * @param freq shake frequency (float)
 * @param cycles repeat number (0-continuously)
 */
extern void vsmd_shake_cycles(unsigned char cid, int spos, int epos, float freq, int cycles);
/**
 * @brief shake between 2 positions
 * @param cid communication id of vsmd device
 * @param spos start position
 * @param epos end position
 * @param freq shake frequency (float)
 * @param time time (sencond) (0-continuously)
 */
extern void vsmd_shake_time(unsigned char cid, int spos, int epos, float freq, int time);
/**
 * @brief shake stop
 * @param cid communication id of vsmd device
 */
extern void vsmd_shake_stop(unsigned char cid);
/**
 * @brief clear encoder error bit
 * @param cid communication id of vsmd device
 */
extern void vsmd_eclr(unsigned char cid);

/**
 * @brief config target speed
 * @param cid communication id of vsmd device
 * @param spd target speed
 */
extern void vsmd_cfg_spd(unsigned char cid, float spd);
/**
 * @brief config acceleration
 * @param cid communication id of vsmd device
 * @param acc acceleration
 */
extern void vsmd_cfg_acc(unsigned char cid, float acc);
/**
 * @brief config deceleration
 * @param cid communication id of vsmd device
 * @param dec deceleration
 */
extern void vsmd_cfg_dec(unsigned char cid, float dec);
/**
 * @brief config accelerating-state current
 * @param cid communication id of vsmd device
 * @param cra accelerating-state current
 */
extern void vsmd_cfg_cra(unsigned char cid, float cra);
/**
 * @brief config normal current
 * @param cid communication id of vsmd device
 * @param crn normal current
 */
extern void vsmd_cfg_crn(unsigned char cid, float crn);
/**
 * @brief config holding-state current
 * @param cid communication id of vsmd device
 * @param cra hoding-state current
 */
extern void vsmd_cfg_crh(unsigned char cid, float crh);
/**
 * @brief config parameter erty
 * @param cid communication id of vsmd device
 * @param erty encoder retry number
 */
extern void vsmd_cfg_erty(unsigned char cid, unsigned char erty);
/**
 * @brief config parameter ewr
 * @param cid communication id of vsmd device
 * @param ewr event while encoder error
 */
extern void vsmd_cfg_ewr(unsigned char cid, unsigned char ewr);
/**
 * @brief config parameter esds
 * @param cid communication id of vsmd device
 * @param esds encoder stall-detection sensitivity (0.1-0.9)
 */
extern void vsmd_cfg_esds(unsigned char cid, float esds);

/**
 * @brief check motor driver online status
 * @param cid communication id of vsmd device
 * @retval 0-offline
 *         1-online
 */
extern unsigned char vsmd_is_online(unsigned char cid);
/**
 * @brief get actual speed
 * @param cid communication id of vsmd device
 * @retval actual speed
 */
extern float vsmd_get_speed(unsigned char cid);
/**
 * @brief get actual position
 * @param cid communication id of vsmd device
 * @retval actual position
 */
extern int vsmd_get_position(unsigned char cid);
/**
 * @brief get status bits
 * @param cid communication id of vsmd device
 * @retval status bits
 */
extern unsigned int vsmd_get_status(unsigned char cid);

/**
 * @brief vsmd lib initialize
 */
extern void vsmd_init(void);
/**
 * @brief create vsmd driver
 * @param cid communication id of vsmd device
 */
extern void vsmd_create_device(unsigned char cid);
/**
 * @brief vsmd main process (the method need be called in main loop or task(realtime-os))
 */
extern void vsmd_run(void);
/**
 * @brief get milliseconds since power on
 * @param milliseconds
 */
extern unsigned long vsmd_get_ms(void);

#endif

